/**
 * 
 */
package net.sos.translator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * @author SANJAY
 *
 */
public class TextFileTranslatorImpl implements ITextFileTranslator {

   protected ITranslator translator;
   
   /* (non-Javadoc)
    * @see net.sos.translator.ITextFileTranslator#setTranslator(net.sos.translator.ITranslator)
    */
   @Override
   public void setTranslator(ITranslator translator) {      
      this.translator = translator;
   }

   /* (non-Javadoc)
    * @see net.sos.translator.ITextFileTranslator#translateFile(java.io.File, java.io.File)
    */
   @Override
   public void translateFile(File inFile, File outFile) throws TranslationException {
      try {
         final String NEW_LINE = System.getProperty("line.separator");
         BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(inFile)));
         PrintWriter out = new PrintWriter(outFile);
         try {
            String line = null;
            while((line = in.readLine()) != null) {
               out.write(translator.translate(line));
               out.write(NEW_LINE);
            }
         } finally {
            if(in != null) {
               in.close();
            }
            if(out != null) {
               out.flush();
               out.close();
            }
         }
      } catch(Exception e) {
         throw new TranslationException(e.getMessage(), e);
      }
      
   }

}
